import { GetUserOpenId } from "@/api/user";
import Taro from "@tarojs/taro";
import { useState } from "react";
import { useRequest } from 'taro-hooks';

const localOpenId = Taro.getStorageSync("openId") || undefined
export default function useOpenId() {
    const [openId, setOpenId] = useState<string | undefined>(localOpenId)
    useRequest(GetUserOpenId, {
        ready: !localOpenId,
        onSuccess: function (data) {
            setOpenId(data)
            Taro.setStorageSync("openId", data)
        }
    })

    return {
        openId
    }
}